home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / misc / AmigaSDLsrc.lha / amisrc / SDL_cgxyuv.c < prev    next >
C/C++ Source or Header  |  2000-08-11  |  5KB  |  192 lines

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23. #ifdef SAVE_RCSID
  24. static char rcsid =
  25.  "@(#) $Id: SDL_x11yuv.c,v 1.1.2.1 2000/04/10 02:49:41 hercules Exp $";
  26. #endif
  27.  
  28. /* This is the XFree86 Xv extension implementation of YUV video overlays */
  29.  
  30. #ifdef XFREE86_XV
  31.  
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <X11/Xlib.h>
  35. #include <sys/ipc.h>
  36. #include <sys/shm.h>
  37. #include <X11/extensions/XShm.h>
  38. #include <X11/extensions/Xvlib.h>
  39.  
  40. #include "SDL_error.h"
  41. #include "SDL_video.h"
  42. #include "SDL_x11yuv_c.h"
  43. #include "SDL_yuvfuncs.h"
  44.  
  45. /* The functions used to manipulate software video overlays */
  46. static struct private_yuvhwfuncs x11_yuvfuncs = {
  47.     X11_LockYUVOverlay,
  48.     X11_UnlockYUVOverlay,
  49.     X11_DisplayYUVOverlay,
  50.     X11_FreeYUVOverlay
  51. };
  52.  
  53. struct private_yuvhwdata {
  54.     int port;
  55.     XShmSegmentInfo yuvshm;
  56.     XvImage *image;
  57. };
  58.  
  59.  
  60. SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
  61. {
  62.     SDL_Overlay *overlay;
  63.     struct private_yuvhwdata *hwdata;
  64.     int xv_port;
  65.     int i, j;
  66.     int adaptors;
  67.     XvAdaptorInfo *ainfo;
  68.     XShmSegmentInfo *yuvshm;
  69.  
  70.     xv_port = -1;
  71.     if ( (Success == XvQueryExtension(GFX_Display, &j, &j, &j, &j, &j)) &&
  72.          (Success == XvQueryAdaptors(GFX_Display,
  73.                                      RootWindow(GFX_Display, SDL_Screen),
  74.                                      &adaptors, &ainfo)) ) {
  75.         for ( i=0; (i<adaptors) && (xv_port == -1); ++i ) {
  76.             if ( (ainfo[i].type & XvInputMask) &&
  77.                  (ainfo[i].type & XvImageMask) ) {
  78.                 int num_formats;
  79.                 XvImageFormatValues *formats;
  80.                 formats = XvListImageFormats(GFX_Display,
  81.                               ainfo[i].base_id, &num_formats);
  82.                 for ( j=0; j<num_formats; ++j ) {
  83.                     if ( (Uint32)formats[j].id == format ) {
  84.                         xv_port = ainfo[i].base_id;
  85.                         break;
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     if ( xv_port == -1 ) {
  92.         SDL_SetError("No available video ports for requested format");
  93.         return(NULL);
  94.     }
  95.  
  96.     /* Create the overlay structure */
  97.     overlay = (SDL_Overlay *)malloc(sizeof *overlay);
  98.     if ( overlay == NULL ) {
  99.         SDL_OutOfMemory();
  100.         return(NULL);
  101.     }
  102.     memset(overlay, 0, (sizeof *overlay));
  103.  
  104.     /* Fill in the basic members */
  105.     overlay->format = format;
  106.     overlay->w = width;
  107.     overlay->h = height;
  108.  
  109.     /* Set up the YUV surface function structure */
  110.     overlay->hwfuncs = &x11_yuvfuncs;
  111.  
  112.     /* Create the pixel data and lookup tables */
  113.     hwdata = (struct private_yuvhwdata *)malloc(sizeof *hwdata);
  114.     overlay->hwdata = hwdata;
  115.     if ( hwdata == NULL ) {
  116.         SDL_OutOfMemory();
  117.         SDL_FreeYUVOverlay(overlay);
  118.         return(NULL);
  119.     }
  120.     yuvshm = &hwdata->yuvshm;
  121.     memset(yuvshm, 0, sizeof(*yuvshm));
  122.     hwdata->port = xv_port;
  123.     hwdata->image = XvShmCreateImage(GFX_Display, xv_port, format,
  124.                                      0, width, height, yuvshm);
  125.     if ( hwdata->image == NULL ) {
  126.         SDL_OutOfMemory();
  127.         SDL_FreeYUVOverlay(overlay);
  128.         return(NULL);
  129.     }
  130.     yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
  131.                            IPC_CREAT | 0777);
  132.     if ( yuvshm->shmid < 0 ) {
  133.         SDL_SetError("Unable to get %d bytes shared memory",
  134.                      hwdata->image->data_size);
  135.         SDL_FreeYUVOverlay(overlay);
  136.         return(NULL);
  137.     }
  138.     yuvshm->shmaddr  = (caddr_t) shmat(yuvshm->shmid, 0, 0);
  139.     yuvshm->readOnly = False;
  140.     hwdata->image->data = yuvshm->shmaddr;
  141.  
  142.     XShmAttach(GFX_Display, yuvshm);
  143.     XSync(GFX_Display, False);
  144.     shmctl(yuvshm->shmid, IPC_RMID, 0);
  145.  
  146.     /* We're all done.. */
  147.     return(overlay);
  148. }
  149.  
  150. int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
  151. {
  152.     overlay->pixels = overlay->hwdata->image->data;
  153.     /* What should the pitch be set to? */
  154.     return(0);
  155. }
  156.  
  157. void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
  158. {
  159.     overlay->pixels = NULL;
  160. }
  161.  
  162. int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
  163. {
  164.     struct private_yuvhwdata *hwdata;
  165.  
  166.     hwdata = overlay->hwdata;
  167.     XvShmPutImage(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
  168.                   hwdata->image, 0, 0, overlay->w, overlay->h,
  169.                   dstrect->x, dstrect->y, dstrect->w, dstrect->h, False);
  170.     XSync(GFX_Display, False);
  171.     return(0);
  172. }
  173.  
  174. void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
  175. {
  176.     struct private_yuvhwdata *hwdata;
  177.  
  178.     hwdata = overlay->hwdata;
  179.     if ( hwdata ) {
  180.         if ( hwdata->yuvshm.shmaddr ) {
  181.             XShmDetach(GFX_Display, &hwdata->yuvshm);
  182.             shmdt(hwdata->yuvshm.shmaddr);
  183.         }
  184.         if ( hwdata->image ) {
  185.             XFree(hwdata->image);
  186.         }
  187.         free(hwdata);
  188.     }
  189. }
  190.  
  191. #endif /* XFREE86_XV */
  192.